package com.donn.securitycontrolplatform.hisalarm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dahuatech.icc.exception.ClientException;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.db1.mapper.THisAlarmMapper;
import com.donn.securitycontrolplatform.equipment.entity.TEquipment;
import com.donn.securitycontrolplatform.equipment.service.ITEquipmentService;
import com.donn.securitycontrolplatform.hisalarm.entity.THisAlarm;
import com.donn.securitycontrolplatform.hisalarm.entity.dto.HisAlarmMonth;
import com.donn.securitycontrolplatform.hisalarm.service.ITHisAlarmService;
import com.donn.securitycontrolplatform.utils.DaHuaUtil;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhenglx
 * @since 2022-06-26
 */
@Service
public class THisAlarmServiceImpl extends ServiceImpl<THisAlarmMapper, THisAlarm> implements ITHisAlarmService {

    @Override
    public Result getHisAlarmById(String hisAlarmId) {
        try {
            return Result.success(getById(hisAlarmId));
        } catch (Exception e) {
            log.error("An error occurred in the getHisAlarmById : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    @Override
    public Result selectHisAlarmList(THisAlarm hisAlarm, Page page) {
        try {
            QueryWrapper<TEquipment> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(TEquipment::getTagId, hisAlarm.getTagid());
            TEquipment equipment = equipmentService.getOne(wrapper);
            if (equipment != null && equipment.getRemark().equals("高压气瓶")) {
                QueryWrapper<THisAlarm> queryWrapper = new QueryWrapper<>();
                if (StringUtil.isNotEmpty(hisAlarm.getEquipmentNum())) {
                    queryWrapper.lambda().eq(THisAlarm::getEquipmentNum, hisAlarm.getEquipmentNum());
                }
                return Result.success(page(page, queryWrapper));
            }
        } catch (Exception e) {
            log.error("query error", e);
            return Result.init(500, "service error", e.getMessage());
        }
        return Result.success(null);
    }

    @Override
    public Result deleteHisAlarm(String hisAlarmId) {
        try {
            return Result.success(removeById(hisAlarmId));
        } catch (Exception e) {
            log.error("An error occurred in the deleteHisAlarm : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    @Override
    public Result updateHisAlarm(String hisAlarm) {
        try {
            THisAlarm byId = getById(hisAlarm);
            if (!ObjectUtils.isEmpty(byId)) {
                if (byId.getStatus() == THisAlarm.STATUS_0) {
                    byId.setStatus(THisAlarm.STATUS_1);
                    updateById(byId);
                }

            }
            return Result.success("");
        } catch (Exception e) {
            log.error("An error occurred in the updateHisAlarm : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    @Autowired
    ITEquipmentService equipmentService;

    @Override
    public Result insertHisAlarm(THisAlarm hisAlarm) {
        try {
            return Result.success(saveOrUpdate(hisAlarm));
        } catch (Exception e) {
            log.error("An error occurred in the insertGroup : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    @Override
    public Result getPage(Page<THisAlarm> page) {
        IPage<THisAlarm> result = baseMapper.getPage(page);
        return Result.success(result);
    }

    @Override
    public Result getImg(String imgUri) throws ClientException {

        String token = DaHuaUtil.userpwdAuth();
        StringBuilder sb = new StringBuilder();
        sb.append("https://")
                .append(DaHuaUtil.HOST)
                .append(":")
                .append(DaHuaUtil.HTTPS)
                .append("/evo-apigw/evo-oss/")
                .append(imgUri)
                .append("?token=")
                .append(token);
        String imgPath = sb.toString();
        return Result.success(imgPath);
    }

    @Override
    public Result getYearData() {

        List<HisAlarmMonth> data = baseMapper.getYearData();

        return Result.success(data);
    }

    @Override
    public Result getDayData() {
        List<HisAlarmMonth> data = baseMapper.getDayData();

        return Result.success(data);
    }

    @Override
    public Result getYearDataByViolation() {

        List<HisAlarmMonth> data = baseMapper.getYearDataByViolation();

        return Result.success(data);
    }
}
